from PyQt4 import QtGui, QtCore, Qt

class PreferencesWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setWindowTitle(self.tr("Preferences"))

        #main layout
        self.mainLayout = QtGui.QVBoxLayout()
        self.setLayout(self.mainLayout)
        
        self.createPathsGroup()
        
        self.readSettings()
#        self.mainLayout.addStretch(1)

    def closeEvent(self, event):
        self.writeSettings()
        event.accept()

    def readSettings(self):
        settings = QtCore.QSettings()
        pluginspath = settings.value("plugins_path", QtCore.QVariant(QtCore.QString("./plugins"))).toString()
        configspath = settings.value("configs_path", QtCore.QVariant(QtCore.QString("./configs"))).toString()
        docspath = settings.value("docs_path", QtCore.QVariant(QtCore.QString("/usr/share/doc"))).toString()
        self.pluginsEdit.setText(pluginspath)
        self.configsEdit.setText(configspath)
        self.docsEdit.setText(docspath)
    
    def writeSettings(self):
        settings = QtCore.QSettings()
        settings.setValue("plugins_path", QtCore.QVariant(self.pluginsEdit.text()))
        settings.setValue("configs_path", QtCore.QVariant(self.configsEdit.text()))
        settings.setValue("docs_path", QtCore.QVariant(self.docsEdit.text()))

    def createPathsGroup(self):
        pathsGroup = QtGui.QGroupBox(self.tr("Paths"))
        pathsGroupLayout = QtGui.QVBoxLayout()
        pathsGroup.setLayout(pathsGroupLayout)
        self.mainLayout.addWidget(pathsGroup)

        #plugins layout
        pluginsLayout = QtGui.QHBoxLayout()
        pathsGroupLayout.addLayout(pluginsLayout)

        #plugins label
        pluginsLabel = QtGui.QLabel(self.tr("Plugins:"))
        pluginsLayout.addWidget(pluginsLabel)

        #plugins edit
        self.pluginsEdit = QtGui.QLineEdit()
        pluginsLayout.addWidget(self.pluginsEdit)
        
        #configs layout
        configsLayout = QtGui.QHBoxLayout()
        pathsGroupLayout.addLayout(configsLayout)
        
        #configs label
        configsLabel = QtGui.QLabel(self.tr("Configuration Files:"))
        configsLayout.addWidget(configsLabel)

        #configs edit
        self.configsEdit = QtGui.QLineEdit()
        configsLayout.addWidget(self.configsEdit)
        
        #docs layout
        docsLayout = QtGui.QHBoxLayout()
        pathsGroupLayout.addLayout(docsLayout)
        
        #docs label
        docsLabel = QtGui.QLabel(self.tr("Documentation:"))
        docsLayout.addWidget(docsLabel)

        #docs edit
        self.docsEdit = QtGui.QLineEdit()
        docsLayout.addWidget(self.docsEdit)
        
